<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_guashop/model')->方法名();
*/
namespace jy_guashop;
defined('BY_JYA') or exit('error');
class model extends \plugin{
    public function up_canlog($item){
        $data=array(
            'uniacid'=>$_SESSION['uniacid'],
            'mid'=>$item['mid']?:$_SESSION['uid'],
            'goodsid'=>$item['goodsid'],
            'fee'=>$item['fee'],
            'dayid'=>date('Y-m-d'),
            'sendtype'=>$item['sendtype'],
            'address'=>$item['address'],
            'createtime'=>TIMESTAMP,
            'status'=>0,
        );
        $this->insert('canlog',$data);
        $canlogid=pdo_insertid();
        return $canlogid;
    }
    public function canlog_pay($canlog,$params=array()){
        $update=array(
            'paytype'=>$params['paytype'],
            'transid'=>$params['transid'],
            'ispay'=>1,
            'status'=>1,
            'order_status'=>1,
        );
        if($this->setting['seller']){
            foreach($this->setting['seller'] as $sv){
               if($sv['mid']==$canlog['mid']){
                   $update['status']=4;
                   $update['order_status']=4;
                }
            }
        }
        $this->pdo_update('canlog',$update,array('id'=>$canlog['id']));
        $this->pdo_update('goods',array('status'=>2,'successtime'=>TIMESTAMP),array('id'=>$canlog['goodsid']));
        $goods=pdo_get('jy_guashop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$canlog['goodsid']));
        $fee=$canlog['fee'];
        if($this->setting['guafee_rate'] && $canlog['fee']){
            $fee=$this->setting['guafee_rate']*$canlog['fee']/100;
        }
        model('core/member')->member_credit($goods['mid'],'credit2',$fee,add,'jy_guashop','挂卖销售成功收益');
        $canlog['canlogid']=$canlog['id'];
        $this->add_feelog($canlog,7);
        $member=$this->get('member',array('mid'=>intval($canlog['mid'])),'fromuser');
        if($member['fromuser']){
            $this->model('member')->update_agent_level($member['fromuser']);
        }
        return true;
    }
    public function check_buy($goods,$post=array(),$isyue=false,$member=array()){
        return true;
    }
	public function add_feelog($item,$type=0){
		$hourkey=$this->gethourkey();
		$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$item['mid']?:$_SESSION['uid'],
					'goodsid'=>$item['goodsid'],
					'canlogid'=>$item['canlogid']?:$item['id'],
					'yuelogid'=>$item['yuelogid'],
					'fee'=>$item['fee'],
					'hourkey'=>$hourkey,
					'dayid'=>date('Y-m-d'),
					'type'=>$type,
					'createtime'=>TIMESTAMP,
					'status'=>0,
		);
		$this->insert('feelog',$data);
		return true;
	}
	public function redfeelog($canlog=array()){
		$game=pdo_get('jy_weishop_game',array('action'=>'jy_guashop/game/','plugin_name'=>'jy_guashop','uniacid'=>$_SESSION['uniacid']));
		$game['reward']=$game['reward']?json_decode($game['reward'],true):array();
		if(!$game['reward'] || !$game['id']){
			return false;
		}
		$allfee=$canlog['fee'];
		foreach($game['reward'] as $k=>$item){
			if(!$item['redfee_rate'] || $item['redfee_rate']<=0){
				continue;
			}
			$fee=$allfee*$item['redfee_rate']/100;
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'goodsid'=>$canlog['goodsid'],
				'fee'=>$fee,
				'hourkey'=>$canlog['hourkey'],
				'dayid'=>$canlog['dayid'],
				'reward_key'=>$k,
				'createtime'=>TIMESTAMP,
				'status'=>0,
			);
			$this->insert('redfeelog',$data);
		}
		return true;
	}
	public function gongfeelog($params,$allfee=0){
		$gong_fee=$this->setting['gong_fee'];
		if(!$gong_fee || $allfee<=0){
			return false;
		}
		$fee=$allfee*$gong_fee/100;
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'goodsid'=>$params['goodsid'],
			'fee'=>$fee,
			'hourkey'=>$params['hourkey'],
			'dayid'=>$params['dayid'],
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		$this->insert('gongfeelog',$data);
		return true;
	}
	public function team_level_up_types(){
		$teamsetting=$this->plugin_setting('teamsetting');
		$set=str_array($teamsetting['level_up_types']);
		$level_up_types=$this->level_up_types;
		foreach($set as $k=>$type){
			if(!$level_up_types[$type]){
				unset($set[$k]);
			}
		}
		return $set;
	}
	public function current_level_up_types(){
		$areasetting=$this->plugin_setting('areasetting');
		return str_array($areasetting['level_up_types']);
	}
}
?>